function [t, PosSat, Dobs, PobsL1, PobsL2, PobsL3, Dtropo, ElevSat, sat_const, sat_PRN, result_LS_code_base,result_LS_code_rover]=interf_calc_preprocessing_phase(RNX_header_base, RNX_data_base, RNX_header_rover, RNX_data_rover, NAV_header, NAV_data, X0_base, epoch, options)
%% function [t, PosSat, Dobs, PobsL1, PobsL2, PobsL3, Dtropo, ElevSat, sat_const, sat_PRN, result_LS_code_base,result_LS_code_rover]=interf_calc_preprocessing_phase(RNX_header_base, RNX_data_base, RNX_header_rover, RNX_data_rover, NAV_header, NAV_data, X0_base, epoch, options)
%% Data organization and preparation before phase processing - interface function
%%
%% Clement Fontaine - 2014-01-28
%%
%% Input
%%	- RNX_header_base, RNX_data_base : obs rinex data for base station generated by function load_rinex_o
%%	- RNX_header_rover, RNX_data_rover : obs rinex data for rover station generated by function load_rinex_o
%%  - NAV_header,NAV_data : nav data (rinex_n or sp3) generated by load_rinex_n (for broadcasted ephemeris) or load_sp3 (for precise orbits)
%% 		- broadcasted ephemeris : NAV_header,NAV_data generated from broadcasted ephemeris .n or .p
%% 		- precise orbits : replace NAV_header,NAV_data by sp3_header,sp3_data, and set options.nav to 'sp3'
%%  - X0_base : base station coordinates
%%	- epoch : computation epoch
%%	- options : structure containing processing options ( optional )
%% {
%%   X0 : approximated coordinates (column vector of 6 elements [X;Y;Y;cdtr;cGGTO;cGPGL]) 
%%        default : X0 = [0 ;0; 0; 0; 0; 0];
%%   const : constellations used ('G' = GPS, 'R' = Glonass, 'E' = Galileo, for multi-constellation concatenate chars)
%%        default : 'G'
%%   nav : type of orbits
%%		   - 'brdc' : broadcasted ephemeris
%%         - 'sp3' : precise orbits
%%         default : 'brdc'    
%%   cut_off : elevation cut off in degree
%%         default : 3 degrees
%% }
%%
%% Output : 
%% - t : obs mjd
%% - PosSat : matrix containing satellite position [Xs, Ys, Zs] (m)
%% - Dobs : corrected pseudo-range obs in m (one line = one satellite, one column = one receiver [base rover])
%% - PobsL1, PobsL2, PobsL3 : L1, L2 and L3 obs in m (one line = one satellite, one column = one receiver [base rover])
%% - Dtropo : tropospheric delay correction (m) (one line = one satellite, one column = one receiver [base rover])
%% - ElevSat : satellite elevation (rad) (one line = one satellite, one column = one receiver [base rover])
%% - sat_const : vector containing constellation type (1 = GPS, 2 = Galileo, 3 = Glonass)
%% - sat_PRN : vector containing PRN of satellite
%% - result_LS_code_base / result_LS_code rover : LS code estimation result structure. See calc_LS_code() for details
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
c = 299792458.0;

if nargin == 9
	if isfield(options,'X0')
		X0 = options.X0;
		if length(X0)<6
			X0 = [X0(:);zeros(6-length(X0),1)];
		end
		options.X0 = X0;
	else
		options.X0 = [0;0;0;0;0;0];
	end
else
	options.X0 = [0;0;0;0;0;0];
end


X0_base = [X0_base(1:3);0]; %cdtr

% initial cGGTO and cGPGL

if isfield(NAV_header,'GPGA')
	cGGTO = c * NAV_header.GPGA(1);  % Approx GGTO, not the real formula which need mjd (cf official doc Galileo)
	X0_base = [X0_base;cGGTO];  
else
	X0_base = [X0_base;0];  
end

if isfield(NAV_header,'GLGP')
	cGPGL = c * NAV_header.GLGP(1); % in rinex, GPGL is not defined, but  - GLGP is present
	X0_base = [X0_base;cGPGL];  
else
	X0_base = [X0_base;0];  
end
	
[G1,G2,result,result_LS_code_base,result_LS_code_rover]=calc_preprocessing_phase(RNX_header_base, RNX_data_base, RNX_header_rover, RNX_data_rover, NAV_header, NAV_data, X0_base, epoch, options);


% output
PosSat = result.PosSat(:,1:3);

Dobs = result.Dobs;

PobsL1 = result.PobsL1;
PobsL2 = result.PobsL2;
PobsL3 = result.PobsL3;

ElevSat = result.ElevSat;
Dtropo = result.Dtropo;

sat_const = zeros(size(Dobs,1),1);
sat_PRN = zeros(size(Dobs,1),1);

for i = 1:size(result.sat_index,1)

	id = result.sat_index{i,1};	
	sat_PRN(i) = str2num(id(2:3));

	if (id(1) == 'G')
		sat_const(i) = 1;	
	elseif (id(1) == 'E')
		sat_const(i) = 2;
	elseif (id(1) == 'R')
		sat_const(i) = 3;
	end
	
end

t = result.t;


end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

